﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ReBiaoDTU
{
    public partial class DTURunInfo : Form
    {
        static public void WaitMS(int ms)
        {
            Thread t = new Thread(o => Thread.Sleep(ms));
            t.Start(new object());
            while (t.IsAlive)
            {
                Application.DoEvents();
            }
            //             while (ms != 0)
            //             {
            //                 Application.DoEvents();
            //                 Thread.Sleep(1);
            //                 ms--;
            //             } 
        }
        public DTURunInfo()
        {
            InitializeComponent();
        }
        public bool IsClose = false;
        public bool IsStopShow = false;
        private delegate void ShowRxDelegate(string str);
        private delegate void ShowListRxDelegate(GPRS_USER_INFO user_info, GPRS_DATA_RECORD recdPtr, int type);
        object sync = new object();
        object sync1 = new object();
        private void ShowRxDelegateProc(string str)
        {
            str = DateTime.Now.ToString() + ":  " + str;
            tbLog.AppendText(str);
        }
        public void ExternShowRx(string str)
        {
            if (IsClose)
                return;
            if (IsStopShow)
                return;
            try
            {
                lock (sync)
                {
                    this.Invoke(new ShowRxDelegate(ShowRxDelegateProc), str + "\r\n");
                }
            }
            catch { }
        }
        private void ShowListRxDelegateProc(GPRS_USER_INFO user_info, GPRS_DATA_RECORD recdPtr, int type)
        {
            ushort usPort;
            switch (type)
            {
                case 0x01:	//注册包
                    bool bHave = false;
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        if (listView1.Items[i].Text == user_info.m_userid)
                        {
                            listView1.Items[i].SubItems.Add(user_info.m_logon_date);
                            listView1.Items[i].SubItems.Add(user_info.m_update_time.ToString());
                            listView1.Items[i].SubItems.Add(HongDianDTU.inet_ntoa(HongDianDTU.ntohl(user_info.m_sin_addr)));
                            usPort = user_info.m_sin_port;
                            listView1.Items[i].SubItems.Add(usPort.ToString());
                            listView1.Items[i].SubItems.Add(HongDianDTU.inet_ntoa(HongDianDTU.ntohl(user_info.m_local_addr)));
                            usPort = user_info.m_local_port;
                            listView1.Items[i].SubItems.Add(usPort.ToString());
                            bHave = true;
                            break;
                        }
                    }
                    if (!bHave)
                    {
                        //没有注册过
                        ListViewItem lvi = listView1.Items.Add(user_info.m_userid);
                        lvi.SubItems.Add(HongDianDTU.inet_ntoa(HongDianDTU.ntohl(user_info.m_local_addr)));

                        usPort = user_info.m_local_port;
                        lvi.SubItems.Add(usPort.ToString());

                        lvi.SubItems.Add(user_info.m_logon_date);

                        lvi.SubItems.Add(HongDianDTU.inet_ntoa(HongDianDTU.ntohl(user_info.m_sin_addr)));

                        usPort = user_info.m_sin_port;
                        lvi.SubItems.Add(usPort.ToString());
                    }
                    break;
                case 0x02:	//注销包                    
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        if (listView1.Items[i].Text == recdPtr.m_userid)
                        {
                            listView1.Items[i].Remove();
                            break;
                        }
                    }
                    break;
            }
            
        }
        public void ExternListShowRx(GPRS_USER_INFO user_info, GPRS_DATA_RECORD recdPtr, int type)
        {
            if (IsClose)
                return;
            if (IsStopShow)
                return;
            try
            {
                lock (sync1)
                {
                    this.Invoke(new ShowListRxDelegate(ShowListRxDelegateProc), user_info, recdPtr, type);
                }
            }
            catch { }
        }
        private void DTURunInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            tbLog.Clear();
        }
    }
}
